<?php
/**
 * Description of PageEdit
 *
 * @author kingop
 */
class FormProcessor_PageEdit extends FormProcessor {
    protected $db = null;               // объект для соединения с базой данных
    public $page = null;                // объект класса DatabaseObject_User
    protected $_validateOnly = false;   // флаг, указывающий тип запроса: true = ajax
    
    public function __construct($db, $id) {
        parent::__construct();
        $this->db = $db;
        $this->page = new DatabaseObject_Pages($this->db); 
        $this->page->load($id);
        $this->name = $this->page->name;
        $this->title = $this->page->title;
        $this->keywords = $this->page->keywords;
        $this->description = $this->page->description;
        $this->h1 = $this->page->h1;
        $this->content = $this->page->content;
        $this->alias = $this->page->alias;
        $this->displayInMenu = $this->page->displayInMenu;
    }
    
    public function process(Zend_Controller_Request_Abstract $request) {
        $this->title = trim($request->getPost('title'));

        if (strlen($this->title) == 0)
            $this->addError('title', 'Вы должны указать title');
        else
            $this->page->title = $this->title;
        
        $this->name = trim($request->getPost('name'));

        if (strlen($this->name) == 0)
            $this->addError('name', 'Вы должны указать title');
        else
            $this->page->name = $this->name;
        
        $this->keywords = trim($request->getPost('keywords'));

        if (strlen($this->keywords) == 0)
            $this->addError('keywords', 'Вы должны указать keywords');
        else
            $this->page->keywords = $this->keywords;
        
        $this->description = trim($request->getPost('description'));

        if (strlen($this->description) == 0)
            $this->addError('description', 'Вы должны указать description');
        else
            $this->page->description = $this->description;
        
        $this->h1 = trim($request->getPost('h1'));

        if (strlen($this->h1) == 0)
            $this->addError('h1', 'Вы должны указать h1');
        else
            $this->page->h1 = $this->h1;
        
        $this->content = trim($request->getPost('content'));

        if (strlen($this->content) == 0)
            $this->addError('content', 'Содержимое страницы не может ыть пустым');
        else
            $this->page->content = $this->content;
        
        $this->alias = trim($request->getPost('alias'));

        if (strlen($this->alias) == 0)
            $this->addError('alias', 'Вы должны указать ссылку');
        else
            $this->page->alias = $this->alias;
        
        if($request->getPost('displayInMenu')) {
            $this->page->displayInMenu = 1;
            $this->displayInMenu = 1;
        }
        else {
            $this->page->displayInMenu = 0;
            $this->displayInMenu = 0;
        }
        
        // если не было ошибок, сохраним пользователя
        if (!$this->_validateOnly && !$this->hasError()) {
            $this->page->save();
        }
        
        return !$this->hasError();
    }
}

?>
